package com.chap1;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

/**
 * <p>
 * <code>Piped</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-03-23 14:29
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class Piped {
    public static void main(String[] args) throws IOException {
        PipedWriter out = new PipedWriter();
        PipedReader in = new PipedReader();
        out.connect(in);


        Thread printThread = new Thread(new Print(in),"PrintThread");
        printThread.start();
        int receive = 0;
        try{
            //键盘输入的内容，获取到，输出流接收，写给输入流
            while((receive = System.in.read())!= -1){
                out.write(receive);
            }
        }finally {
            out.close();
        }

    }


    static class Print implements Runnable{
        private PipedReader in;

        public Print(PipedReader in) {
            this.in = in;
        }

        @Override
        public void run() {
            int receive = 0;
            try{
                //输入流 读取输出流写的东西
                while ((receive = in.read())!= -1){
                    System.out.print((char) receive);
                }
            }catch (Exception e){

            }
        }
    }
}
